package com.drone.drone_server.controller;

import com.drone.drone_server.api.ApiResponse;
import com.drone.drone_server.dao.DroneDao;
import com.drone.drone_server.dao.DroneRouteDao;
import com.drone.drone_server.entity.DroneRoute;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/monitor")
public class FlightMonitorController {

    // 存储无人机指令状态，起飞、降落、开启录制路线、关闭录制路线
    private static final Map<Integer, Object> droneOrder = new ConcurrentHashMap<>();

    // 存储实时遥测数据
    private static final Map<Integer, Map<String, Object>> telemetryData = new ConcurrentHashMap<>();

    //存储实时速度控制数据，x轴速度、y轴速度、旋转速度、时间戳
    private static final Map<Integer, Map<String, Long>> droneFlyControlData = new ConcurrentHashMap<>();

    @Autowired
    private DroneRouteDao droneRouteDao;

    @Autowired
    private DroneDao droneDao;

    @PutMapping("/putDroneOrder")
    public ApiResponse putDroneOrder(@RequestParam Integer drone_id,@RequestBody Object[] objects){
        droneOrder.put(drone_id,objects);
        return ApiResponse.success();
    }

    @GetMapping("/getDroneOrder")
    public ApiResponse getDroneOrder(@RequestParam Integer drone_id){
        return ApiResponse.success(droneOrder.get(drone_id));
    }

    // 遥测数据接收通道
    @PutMapping("/putTelemetry")
    public ApiResponse putTelemetry(@RequestParam Integer drone_id,
                                        @RequestBody  Map<String, Object> data) {
        telemetryData.put(drone_id, data);
        return ApiResponse.success();
    }

    // 状态数据获取通道
    @GetMapping("/getTelemetry")
    public ApiResponse getTelemetry(@RequestParam Integer drone_id) {
        return ApiResponse.success(telemetryData.get(drone_id));
    }

    // 遥控数据接收通道
    @PutMapping("/putFlyControlData")
    public ApiResponse putFlyControlData(@RequestParam Integer drone_id,
                                    @RequestBody  Map<String, Long> data) {
        droneFlyControlData.put(drone_id, data);
        return ApiResponse.success();
    }

    // 遥控数据获取通道
    @GetMapping("/getFlyControlData")
    public ApiResponse getFlyControlData(@RequestParam Integer drone_id) {
        return ApiResponse.success(droneFlyControlData.get(drone_id));
    }

    // 路线数据查询通道
    @GetMapping("/getRoute")
    public ApiResponse getRoute(@RequestParam Integer route_id) {
        // 获取航线数据，检查是否存在
        DroneRoute droneRoute = droneRouteDao.selectById(route_id);
        if (droneRoute == null) {
            return ApiResponse.fail("未找到对应的航线");
        }

        // 获取坐标JSON字符串
        String coordinatesJson = droneRoute.getCoordinates();
        ObjectMapper objectMapper = new ObjectMapper();

        try {
            // 解析JSON字符串为JsonNode
            JsonNode coordinatesNode = objectMapper.readTree(coordinatesJson);
            // 返回解析后的JSON数据
            return ApiResponse.success(coordinatesNode);
        } catch (JsonProcessingException e) {
            // 处理JSON格式错误
            return ApiResponse.fail("坐标数据格式错误");
        }
    }

    @GetMapping("/getDroneIdList")
    public ApiResponse getDroneIdList(){
        return ApiResponse.success(droneDao.selectAllIds());
    }

    @GetMapping("/getActiveRoutes")
    public ApiResponse getActiveRoutes(){
        return ApiResponse.success(droneRouteDao.selectActiveIdNameList());
    }
}
